package com.hr.reactive.operators;

import com.hr.reactive.common.Utils;
import reactor.core.publisher.Mono;

import java.time.Duration;

public class Lec07Timeout {
    public static void main(String[] args) {
        getProductName()
                .timeout(Duration.ofSeconds(2),getProductNameFallBack())
                .subscribe(Utils.subscriber());
        Utils.sleepSeconds(5000);

        var mono=Mono.empty().defaultIfEmpty("A");
        mono.defaultIfEmpty("B").subscribe(Utils.subscriber());
    }
    private static Mono<String> getProductName(){
        return Mono.fromSupplier(()->"server "+ Utils.faker().commerce().productName())
                .delayElement(Duration.ofMillis(3));
    }
    private static Mono<String> getProductNameFallBack(){
        return Mono.fromSupplier(()-> Utils.faker().commerce().productName())
                .delayElement(Duration.ofSeconds(1));
    }
}
